-- Client side -This approach does the file handling on the client side
\Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',' HEADER
-- Server side - This approach runs entirely on the remote server
Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',' HEADER;
jq -r '(.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv'
\copy (SELECT * FROM persons) to 'C:\tmp\persons_client.csv' with csv